package cn.xingyun.pickaxe_loli_new.mixin;

import cn.xingyun.pickaxe_loli_new.init.NewLoliPickaxeModItems;
import cn.xingyun.pickaxe_loli_new.item.LolipickaxeItem;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(LivingEntity.class)
public abstract class LivingEntityMixin {
    @Shadow public abstract float getMaxHealth();

    @Inject(method = "getHealth",at = @At("RETURN"),cancellable = true)
    private void getHealth(CallbackInfoReturnable<Float> cir) {
        if ((LivingEntity) (Object) this instanceof Player player && player.getInventory().contains(NewLoliPickaxeModItems.LOLIPICKAXE.get().getDefaultInstance())) {
            //System.out.println("Health:20");
            cir.setReturnValue(getMaxHealth()+20);
        } else if (LolipickaxeItem.isBanned((LivingEntity) (Object) this)) {
            cir.setReturnValue(0f);
            LolipickaxeItem.removeBanEntity((LivingEntity) (Object) this);
        }
    }
    @Inject(method = "isDeadOrDying",at = @At("RETURN"),cancellable = true)
    private void isDeadOrDying(CallbackInfoReturnable<Boolean> cir) {
        if ((LivingEntity) (Object) this instanceof Player player && player.getInventory().contains(NewLoliPickaxeModItems.LOLIPICKAXE.get().getDefaultInstance())) {
            cir.setReturnValue(false);
        }else if (LolipickaxeItem.isBanned((LivingEntity) (Object) this)) {
            cir.setReturnValue(true);
        }
    }
    @Inject(method = "hurt",at = @At("HEAD"),cancellable = true)
    private void hurt(DamageSource source, float amount, CallbackInfoReturnable<Boolean> cir) {
        if ((LivingEntity) (Object) this instanceof Player player && player.getInventory().contains(NewLoliPickaxeModItems.LOLIPICKAXE.get().getDefaultInstance())) {
            cir.setReturnValue(false);
        }
    }
    @Inject(method = "die",at = @At("HEAD"),cancellable = true)
    private void die(DamageSource p_21014_, CallbackInfo ci){
        if ((LivingEntity) (Object) this instanceof Player p && p.getInventory().contains(NewLoliPickaxeModItems.LOLIPICKAXE.get().getDefaultInstance())) {
            ci.cancel();
        }
    }
}
